/*
 * @ProjectName: 编程学习
 * @Copyright:   2019 HangZhou Yi Dev, Ltd. All Right Reserved.
 * @address:     http://Yi.tech
 * @date:        2019/5/7 22:24
 * @email:       xiazhaoyang@live.com
 * @description: 本内容仅限于编程技术学习使用，转发请注明出处.
 */
package com.example.auth.config.security;

import com.example.auth.service.IUserDetailsService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityBuilder;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.SecurityFilterChain;

import javax.annotation.Resource;

/**
 * <p>
 *
 * </p>
 *
 * @author Yi
 * @version v1.0.0
 * @date 2019/5/7 22:24
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify By: {修改人} 2019/5/7
 * @modify reason: {方法名}:{原因}
 * ...
 */
@Configuration
@Order(1)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource(name = "userDetailsService")
    private IUserDetailsService userDetailsService;

    /**
     * 配置路由
     * 如果您的授权服务器也是资源服务器，则还有另一个优先级较低的安全过滤器链控制API资源。
     * 对于那些受访问令牌保护的请求，您需要将其路径与主要面向用户的过滤器链中的路径不匹配，
     * 因此请确保包含一个请求匹配器，该请求匹配器仅选取WebSecurityConfigurer上述非API资源。
     * @param http
     * @throws Exception
     */
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //此处配置哪些请求进行权限校验
        http.requestMatchers()
                // /oauth/authorize link org.springframework.security.oauth2.provider.endpoint.AuthorizationEndpoint
                // 必须登录过的用户才可以进行 oauth2 的授权码申请
                .antMatchers("/login","/","/hello","/logout","/oauth/authorize")
                .and()
                .authorizeRequests()
                .antMatchers("/","/user/register", "/register", "/home")
                .permitAll()
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                //配置登录
                .formLogin()
                .defaultSuccessUrl("/hello")
                .permitAll()
                .and()
                //配置退出登录
                .logout()
                .logoutSuccessUrl("/login")
                .permitAll()
                .and().csrf().disable();
    }

    /**
     * 自定义userDetailService
     * @param auth
     * @throws Exception
     */
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    /**
     * 实例化AuthenticationManager
     * @return
     * @throws Exception
     */
    @Override
    @Bean
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }

    /**
     * 配置前端资源
     * 首页、注册页面、静态资源不作权限校验
     * @param web
     * @throws Exception
     */
    @Override
    public void configure(WebSecurity web) throws Exception {
        web.ignoring()
                .antMatchers("/webjars/**", "/plugins/**","/register","/home");
    }
}